﻿Imports System.Data.SqlServerCe



Public Class Proveedores

    Private Sub Proveedores_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        Me.CenterToScreen()
    End Sub

    Private Sub Proveedores_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.ProductosTableAdapter.Fill(Me.NuevoZooDBDataSet.Productos)

        Me.AgendaTableAdapter.Fill(Me.NuevoZooDBDataSet.Agenda)

        If Login.encargadologin = False Then
            BtEliminar.Enabled = False

        End If

    End Sub


    Private Sub TxtNombre_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtNombre.TextChanged
        Me.AgendaTableAdapter.FillByNombre(Me.NuevoZooDBDataSet.Agenda, TxtNombre.Text)
    End Sub




    Private Sub NuevoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NuevoToolStripMenuItem.Click

        Me.Consultar.SelectTab(1)
        TxTAddCIF.Focus()


    End Sub

    Private Sub EditarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditarToolStripMenuItem.Click

        Me.Consultar.SelectTab(2)
        TxtEdCIF.Focus()

    End Sub


    Private Sub EliminarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EliminarToolStripMenuItem.Click

        Me.Consultar.SelectTab(3)

    End Sub


    Private Sub SalirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalirToolStripMenuItem.Click
        Me.Close()
        Gestion.Show()
    End Sub



    Private Sub DataGridView3_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView3.CellClick

        Dim dr As New DataGridViewRow
        dr = DataGridView3.SelectedRows(0)
        TxtEdCIF.Text = dr.Cells(1).Value.ToString
        TxtEdNom.Text = dr.Cells(2).Value.ToString
        TxtEdTlf.Text = dr.Cells(3).Value.ToString
        TxtEdDom.Text = dr.Cells(4).Value.ToString
        TxtEdObs.Text = dr.Cells(5).Value.ToString

    End Sub

    Private Sub DataGridView4_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView4.CellClick

        Dim dr As New DataGridViewRow
        dr = DataGridView4.SelectedRows(0)
        LbCIF.Text = dr.Cells(1).Value.ToString
        LbNombre.Text = dr.Cells(2).Value.ToString
        LbTlf.Text = dr.Cells(3).Value.ToString
        LbDomicilio.Text = dr.Cells(4).Value.ToString
        LbObs.Text = dr.Cells(5).Value.ToString

    End Sub



    Private Sub TextBox16_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox16.TextChanged
        Try
            Me.AgendaTableAdapter.FillByNombre(Me.NuevoZooDBDataSet.Agenda, TextBox16.Text)
            Dim dr As New DataGridViewRow
            dr = DataGridView4.SelectedRows(0)
            LbCIF.Text = dr.Cells(1).Value.ToString
            LbNombre.Text = dr.Cells(2).Value.ToString
            LbTlf.Text = dr.Cells(3).Value.ToString
            LbDomicilio.Text = dr.Cells(4).Value.ToString
            LbObs.Text = dr.Cells(5).Value.ToString

        Catch ex As Exception

        End Try

    End Sub

    Private Sub TextBox17_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox17.TextChanged
        Try
            Me.AgendaTableAdapter.FillByNombre(Me.NuevoZooDBDataSet.Agenda, TextBox17.Text)
            Dim dr As New DataGridViewRow
            dr = DataGridView3.SelectedRows(0)
            TxtEdCIF.Text = dr.Cells(1).Value.ToString
            TxtEdNom.Text = dr.Cells(2).Value.ToString
            TxtEdTlf.Text = dr.Cells(3).Value.ToString
            TxtEdDom.Text = dr.Cells(4).Value.ToString
            TxtEdObs.Text = dr.Cells(5).Value.ToString
        Catch ex As Exception

        End Try


    End Sub


    Private Sub BtAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtAdd.Click

        Dim dr As New DataGridViewRow
        dr = DataGridView3.SelectedRows(0)
        Dim nombre, cif, domicilio, observaciones As String
        Dim tlf As Integer
        Try

        
        cif = TxTAddCIF.Text
        nombre = TxtAddNombre.Text
        tlf = CInt(TxtAddTlf.Text)
        domicilio = TxtAddDom.Text
        observaciones = TxtAddObs.Text

        If Login.validar.ValidarNombre(nombre) <> 3 Or nombre = "" Then
            MsgBox("Nombre incorrecto", , "Error")
        ElseIf Login.validar.ValidarCIF(cif) <> 3 Then
            MsgBox("Cif incorrecto", , "Error")
        ElseIf Login.validar.ValidarTlf(tlf) <> 3 Then
            MsgBox("teléfono incorrecto", , "Error")

        Else



                Dim query As IEnumerable(Of Decimal) = _
                        From item As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
                        Where item.Cells(0).Value IsNot DBNull.Value _
                        Select CDec(item.Cells(0).Value)

                ' Obtenemos el valor máximo
                '
                Dim id As Integer = query.ToList.Max() + 1

                Try
                    Me.AgendaTableAdapter.Insert(id, TxTAddCIF.Text, TxtAddNombre.Text, TxtAddTlf.Text, TxtAddDom.Text, TxtAddObs.Text)



                    Me.AgendaTableAdapter.Fill(Me.NuevoZooDBDataSet.Agenda)
                Catch ex As Exception

                End Try
            End If
        Catch ex As Exception
            MsgBox("Los datos introducidos no son correctos", , "Error")
        End Try


    End Sub



    Private Sub BtEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtEliminar.Click
        Dim resultado As MsgBoxResult
        resultado = CType(MessageBox.Show("¿Desea eliminar el registro?", "Mensaje de advertencia", MessageBoxButtons.YesNo, MessageBoxIcon.Question), MsgBoxResult)
        If resultado = MsgBoxResult.No Then

        Else
            Dim id As Integer
            Dim dr As New DataGridViewRow
            dr = DataGridView3.SelectedRows(0)
            id = dr.Cells(0).Value

            Try

                Me.AgendaTableAdapter.Delete(id)
                Me.AgendaTableAdapter.Fill(Me.NuevoZooDBDataSet.Agenda)


            Catch ex As Exception
                MsgBox("Error al eliminar los datos", , "Error")
            End Try

        End If

       

       

    End Sub



    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Dim dr As New DataGridViewRow
       

        dr = DataGridView3.SelectedRows(0)
        TxtConId.Text = dr.Cells(0).Value.ToString
        TxtConNIF.Text = dr.Cells(1).Value.ToString
        TxtConNom.Text = dr.Cells(2).Value.ToString
        TxtConTlf.Text = dr.Cells(3).Value.ToString
        TxtConDom.Text = dr.Cells(4).Value.ToString
        TxtConObs.Text = dr.Cells(5).Value.ToString

    End Sub

    Private Sub BtActualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtActualizar.Click
        Dim dr As New DataGridViewRow
        dr = DataGridView3.SelectedRows(0)
        Dim nombre, cif, domicilio, observaciones As String
        Dim tlf, id As Integer

        id = dr.Cells(0).Value



        cif = TxtEdCIF.Text
        nombre = TxtEdNom.Text
        tlf = CInt(TxtEdTlf.Text)
        domicilio = TxtEdDom.Text
        observaciones = TxtEdObs.Text

        If Login.validar.ValidarNombre(nombre) <> 3 Or nombre = "" Then
            MsgBox("Nombre incorrecto", , "Error")
        ElseIf Login.validar.ValidarCIF(cif) <> 3 Then
            MsgBox("Cif incorrecto", , "Error")
        ElseIf Login.validar.ValidarTlf(tlf) <> 3 Then
            MsgBox("teléfono incorrecto", , "Error")

        Else


            Try
                Me.AgendaTableAdapter.UpdateQuery(cif, nombre, tlf, domicilio, observaciones, id)
                Me.AgendaTableAdapter.Fill(Me.NuevoZooDBDataSet.Agenda)
                MsgBox("Registro actualizado correctamente", , "Información")
            Catch ex As Exception
                MsgBox("No se pudo actualizar los datos")
            End Try
        End If
    End Sub

    Private Sub AyudaToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AyudaToolStripMenuItem1.Click
        Dim rutaEjecutable As String = System.IO.Directory.GetCurrentDirectory().ToString
        System.Diagnostics.Process.Start(rutaEjecutable & "\Ayuda.chm")
    End Sub
End Class